<?php

namespace app\airobot;

// 作者：卿鹏
use app\airobot\model\BaseConfig;

class CommonLogic
{
    public static $cnyRate = 7;

    public static function DealAmount($value, $isCny = false)
    {
        if ($value == 0) {
            return 0;
        }
        if ($value == null) {
            return 0;
        }

        if ($isCny) {
            $value = $value * self::$cnyRate;
        }
        if (abs($value) < 1) {
            return self::getPrecision($value, 1);
        }

        $decimal = 0;
        /*if ($value < 100) {
            $decimal = 2;
        }*/
        return number_format($value, $decimal, '.', ',');
    }

    public static function getPrecision($value, $precision)
    {
        if ($value == 0) {
            return $value;
        }
        $valueResult = number_format($value, $precision);
        while ($valueResult == 0) {
            if ($precision >= 8) {
                return 0;
            }
            $precision++;
            $valueResult = number_format($value, $precision);
        }

        if ($precision < 4) {
            $valueResult = number_format($value, 4);
        }

        return $valueResult;
    }

    public static function TruncateString($string, $maxLength = 10)
    {
        // 检查字符串长度是否超过指定的最大长度
        if (strlen($string) > $maxLength) {
            // 使用 substr() 函数截断字符串
            $truncatedString = substr($string, 0, $maxLength);
            // 你可以在这里添加额外的逻辑，比如添加省略号 (...)
            // $truncatedString .= '...';
            return $truncatedString;
        }
        // 如果字符串长度不超过最大长度，则返回原始字符串
        return $string;
    }

    public static function CheckImage($ext)
    {
        if (!in_array($ext, ['webp', 'svg', 'png', 'jpg', 'jpeg', 'gif', 'bmp'])) {
            return false;
        }

        return true;
    }

    public static function GetImageUrl()
    {
        $baseConfigModel = new BaseConfig();
        $baseConfigList = $baseConfigModel->queryList([], [], [], [], [], "", $field = "*", $order = "");
        $imageBoxUrl = "";
        $imageForgeHeadUrl = "";
        $imageProductUrl = "";
        if (count($baseConfigList) > 0) {
            $baseConfigInfo = $baseConfigList[0];
            $imageBoxUrl = $baseConfigInfo['image_host'] . "/box/";
            $imageForgeHeadUrl = $baseConfigInfo['image_host'] . "/forge_head/";
            $imageProductUrl = $baseConfigInfo['image_host'] . "/product/";
        }

        return ['box' => $imageBoxUrl, 'forge_head' => $imageForgeHeadUrl, 'product' => $imageProductUrl];
    }

    public static function EncodeContentUtf8(&$sheetDataValue)
    {
        // 尝试检测文件的编码
        $encoding = mb_detect_encoding($sheetDataValue, ['UTF-8', 'GBK', 'ISO-8859-1', 'WINDOWS-1252', 'ASCII'], true);
        if ($encoding && $encoding !== 'UTF-8') {
            $sheetDataValue = mb_convert_encoding($sheetDataValue, 'UTF-8', $encoding);
        } elseif (!$encoding) {
            $sheetDataValue = mb_convert_encoding($sheetDataValue, 'UTF-8', 'GBK');
        }
    }
}

